import {
    mapState,
    mapActions,
    mapMutations
} from "vuex";
import {
    types,
    openFile
} from "@/utils/index";
import {
    getFormData,
    debounce,
    getInputValue
} from "@/utils/index";

export function _onInput(key, e, item, index, parent, parentIndex) {
    let v = getInputValue(e, item);
    if (item.type === "radio") {
        let range = item.range || [];
        let r = range.find((i) => i.value === v);
        if (r) {
            r.checked = !r.checked;
            if (!item.multiple || r.radio) {
                range.forEach((i) => {
                    if (i !== r) {
                        i.checked = false;
                    }
                });
            }
        }
        this[key].splice(index, 1, parent || item);
    } else if (item.type === "checkbox") {
        let range = item.range || [];
        range.forEach(i => i.checked = v.find(k => k == i.value))
        item.value = v;
        // item.value = range.filter(i => i.checked);
    } else {
        if (
            !item.type || [
                "input",
                "textarea",
                "getLocation",
                "chooseLocation",
                "number",
                "digit",
            ].indexOf(item.type) !== -1
        ) {
            item.value = v;
        } else {
            let rangeKey = item.rangeKey;
            item.val = rangeKey ? v[rangeKey] : v;
        }
        this[key].splice(index, 1, parent || item);
    }
}
export function resetInputValue(i, form) {
    if (form[i.key]) {
        if (i.type == "upload") {
            try {
                i.value = form[i.key] ? [data[i.key]] : [];
            } catch (err) {
                i.value = [];
            }
        } else if (i.type == "picker") {
            if (i.mode == "multiSelector") {
                let tmp = form[i.key] || [];
                let range = i.range || [];
                let r = range.map((i, index) => {
                    if (tmp[index]) {
                        let idx = i.findIndex((c) => c == tmp[index]);
                        return (idx > -1 ? idx : 0);
                    }
                });
                i.value = r;
            } else if (i.mode !== "date" && i.mode !== "datetime") {
                let item = (i.range || []).find((it) => it.value == form[i.key]);
                let index = (i.range || []).findIndex(
                    (it) => it.value == form[i.key]
                );
                console.log(i, item, index, form)
                if (item) {
                    i.value = index;
                    i.val = item.label;
                }
            } else {
                i.value = form[i.key];
                i.val = form[i.key];
            }
        } else if (i.type == "radio") {
            let arr = form[i.key];
            if (!Array.isArray(arr)) {
                arr = [arr];
            }
            arr.forEach((c) => {
                (i.range || []).forEach((r) => {
                    if (r.value == c) {
                        r.checked = true;
                    }
                });
            });
        } else {
            i.value = form[i.key] || "";
            if (typeof i.value === 'number') {
                i.value += ""
            }
        }
    }
}
export const publicMixins = {
    data() {
        let self = this;
        let typeArr = ["error", "info", "success", "warning"];
        const message = function (opt) {
            if (self.$refs.message) {
                let type = opt.type || "success";
                let index = typeArr.indexOf(type);
                let key = typeArr[index > -1 ? index : 0];
                self.$refs.message[key](opt);
            }
        };
        typeArr.forEach((i) => {
            message[i] = message.prototype[i] = function (text) {
                message({
                    title: "",
                    type: i,
                    message: text,
                });
            };
        });
        return {
            message,
            $message: message,
        }
    },
    methods: {
        showToast(opt = {}, icon = "none") {
            let def = {
                title: "",
                content: "",
                icon: "none",
                duration: 1500,
            };
            if (typeof opt === "string") {
                opt = {
                    title: opt,
                    icon,
                };
            }
            let option = Object.assign({}, def, opt);
            return new Promise(function (resolve, reject) {
                uni.showToast({
                    ...option,
                    success(res) {
                        resolve(res);
                        if (types.isFunction(option.success)) {
                            option.success(res);
                        }
                    },
                    fail(err) {
                        reject(err);
                        if (types.isFunction(option.fail)) {
                            option.fail(err);
                        }
                    },
                    complete() {
                        if (types.isFunction(option.complete)) {
                            option.complete(...arguments);
                        }
                    },
                });
            });
        },
        showModal(opt = {}) {
            let def = {
                title: "",
                content: "",
                confirmText: "确认",
                cancelText: "取消",
                confirmColor: "#3F7DFF",
            };
            let option = Object.assign({}, def, opt);
            return new Promise(function (resolve, reject) {
                uni.showModal({
                    ...option,
                    success(res) {
                        resolve(res);
                        if (types.isFunction(option.success)) {
                            option.success(res);
                        }
                    },
                    fail(err) {
                        reject(err);
                        if (types.isFunction(option.fail)) {
                            option.fail(err);
                        }
                    },
                    complete() {
                        if (types.isFunction(option.complete)) {
                            option.complete(...arguments);
                        }
                    },
                });
            });
        },
        onOpenFile: openFile,

    }
}

export const panelMixins = {
    data() {
        return {
            panels: [],
            isChangeNmae: false
        }
    },
    methods: {
        panelComplete() {
            console.error("请实现panelComplete方法!")
        },
        onClickPanelItem(item, index) {
            let act = item.active;
            this.panels.forEach(i => i.active = false);
            item.active = !act;
            this.panels.splice(index, 1, item);
        },
        onClickPanelChild(item, index, child, cIndex) {
            let i = item["children"][cIndex];
            let act = i["active"];
            item.children.forEach(i => i.active = false);
            if (act) {
                item["children"][0]["active"] = true;
                i = item["children"][0];
            } else {
                item["children"][cIndex]["active"] = true;
            }
            if (item.type === 'tree') {
                item.activeId = i.id;
            }
            item.subActive = !!i.id;
            item.active = !item.active;
            if (this.isChangeNmae) {
                item.name = i.name;
            }
            console.log(arguments, "???cc")
            this.dataForm[item.key] = child.value;
            this.page = 1;
            this.panels.splice(index, 1, item);
            this.panelComplete(...arguments);
        },
        onClosePanel() {
            this.panels.forEach(i => i.active = false);
        },
        onClickNav(e, item, index) {
            let i = (this.mainActiveIndex = Number(e.index) || 0);
            this.panels[index].mainActiveIndex = i;
        },
        onClickItem(e, item, index) {
            let target = e.target;
            this.activeId = target.id;
            this.panels[index].activeId = target.id;
            this.panels[index].active = false;
            if (this.isChangeNmae) {
                this.panels[index].name = target.id;
            }
            this.panelComplete(e, item, index);
        },
    }
}